<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="600px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="20">
          <el-form-item label="共享类型" prop="sharetype">
            <el-select v-model="model.sharetype" filterable clearable @change="clearOrgInfo">
              <template v-for="item in codeModel.sharetypeList">
                <el-option v-if="item.code !== '6'" :key="item.code" :label="item.name" :value="item.code.toString()" />
              </template>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="model.sharetype == 5">
        <el-col :span="20">
          <el-form-item label="个人" prop="shareobjects">
            <tl-ref-user v-model="model.shareobjects" :initDisplayData.sync="model.shareobjectsdesc" dataType="code" :singleSelect="false" :externalQueryParam="{qry_whereSql:'EXTENDFIELD1 IS NOT NULL AND ENABLED = 1',qry_nodimission:'0'}" ></tl-ref-user>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-else-if="model.sharetype == 1">
        <el-col :span="20">
          <el-form-item label="部门" prop="shareobjects">
            <tl-ref-dept v-model="model.shareobjects" :initDisplayData.sync="model.shareobjectsdesc" dataType="code" :singleSelect="false"></tl-ref-dept>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row v-else-if="model.sharetype == 4" :queryData="queryUserName(dept.headerids)">
        <el-col :span="20">
          <el-form-item label="直接上级" prop="shareobjects">
            <el-input v-model="model.shareobjectsdesc" :shareobjects="(model.shareobjects = dept.headerids)" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-row v-else-if="model.sharetype == 2">
        <el-col :span="20">
          <el-form-item label="团队" prop="shareobjects">
            <tl-ref-list v-model="model.shareobjects" :initDisplayData.sync="model.shareobjectsdesc" :queryDataUrl="url.queryTeamList" :singleSelect="false" :disabled="disableEditSubmit"></tl-ref-list>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-else-if="model.sharetype == 3">
        <el-col :span="20">
          <el-form-item label="角色" prop="shareobjects">
            <tl-ref-role v-model="model.shareobjects" :initDisplayData.sync="model.shareobjectsdesc" dataType="id" :singleSelect="false"></tl-ref-role>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">确认</el-button>
    </div>
  </el-dialog>
</template>
<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: {},
  data() {
    return {
      moduleTitle: '共享人',
      urlPrefix: '/base/permissionManage/',
      url: {
        // 额外的url
        queryTeamList: 'base/teamInfo/list', // 查询团队
        load: '/base/permissionManage/load',
        loadhname: '/base/permissionManage/loadhname'
      },
      teamRefTitle: '团队选择'
    }
  },
  methods: {
    beforeSubmit() {
      this.model.sourcetype = 'permissionmanage'
      this.model.sourceid = ''
      this.$set(this.model, 'sourceusercodes', this.$store.getters.user.code)
      this.$set(this.model, 'sourceusernames', this.$store.getters.user.name)
      return true
    },
    /** 通过userid获得username */
    queryUserName(headerids) {
      if (!this.url.loadhname) {
        this.$tap.alert('请设置loadhname地址!', 'warning')
        return
      }
      getAction(this.url.loadhname, {
        headerids: headerids
      })
        .then((res) => {
          if (res.success) {
            this.$set(this.model, 'shareobjectsdesc', res.result.headersname)
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
        .catch((err) => {
          this.saveLoading = false
        })
    },
    loaded() {
      // 页面加载完毕执行
      if (this.isAdd) {
        this.$set(this.model, 'sharetype', '5')
      }
    },
    // 当切换类型时，清空信息
    clearOrgInfo() {
      this.$set(this.model, 'shareobjectsdesc', '')
      this.$set(this.model, 'shareobjects', '')
    }
  }
}
</script>
